//
//  FQDBannerHeaderView.m
//  FakeQDaily
//
//  Created by mewxiv on 8/14/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDBannerHeaderView.h"
#import "FQDCyclePageView.h"
#import "FQDBannerArticleFeedView.h"
#import "FQDArticleDetailViewController.h"
#import "FQDRestModel.h"

@interface FQDBannerHeaderView () <FQDCyclePageViewDataSource>

// 顶部图片轮播横幅
@property (nonatomic, strong) FQDCyclePageView *bannerView;

@end

@implementation FQDBannerHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.bannerView = [[FQDCyclePageView alloc] initWithFrame:self.bounds];
        self.bannerView.dataSource = self;
        [self addSubview:self.bannerView];
    }
    return self;
}

- (void)pauseAutoScroll
{
    [self.bannerView pauseCycleScroll];
}

- (void)resumeAutoScroll
{
    [self.bannerView resumeCycleScroll];
}

- (void)setBanners:(NSArray *)banners
{
    if (_banners == banners) {
        return;
    }
    
    _banners = banners;
    
    [self.bannerView reloadData];
    
    WeakSelf;
    [self.bannerView setSelectPageItemBlock:^(NSInteger idx) {
        FQDFeed *feed = weakSelf.banners[idx];
        FQDArticleDetailViewController *articleDetailVC =
        [[FQDArticleDetailViewController alloc] init];
        
        articleDetailVC.post = feed.post;
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UINavigationController *navVC = (UINavigationController *)window.rootViewController;
        [navVC pushViewController:articleDetailVC animated:YES];
    }];
}

+ (NSString *)kind
{
    return UICollectionElementKindSectionHeader;
}

+ (NSString *)reuseId
{
    return @"FQDBannerHeaderViewId";
}

#pragma mark - FQDCyclePageView DataSource methods
- (NSInteger)numberOfPagesInPageView:(FQDCyclePageView *)pageView
{
    return [self.banners count];
}

- (UIView *)pageView:(FQDCyclePageView *)pageView viewForPageAtPageIndex:(NSInteger)pageIndex
{
    FQDFeed *banner = self.banners[pageIndex];
    
    FQDBannerArticleFeedView *feedview = [[FQDBannerArticleFeedView alloc] init];
    feedview.banner = banner;
    return feedview;
}

@end
